package cn.edu.scut.sys.service.impl;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Component;

import cn.edu.scut.sys.dao.StudentDao;
import cn.edu.scut.sys.service.StudentService;
import cn.edu.scut.sys.utils.DataRequest;
import cn.edu.scut.sys.utils.DataResponse;
import cn.edu.scut.sys.vo.Student;

@Component("studentService")
public class StudentServiceImpl implements StudentService{

	private StudentDao studentDao;
	
	@Resource(name="studentDao")
	public void setUserDao(StudentDao studentDao) {
		this.studentDao = studentDao;
	}

	@Override
	public DataResponse<Student> list(DataRequest dataRequest, HttpServletRequest request) {
		DataResponse<Student> response = new DataResponse<Student>();  
        int count;//总记录数  
        int limit = dataRequest.getRows() <= 0 ? 20 : dataRequest.getRows();//每页显示数量  
        int totalPages;//总页数  
        int page = dataRequest.getPage() <= 0 ? 1 : dataRequest.getPage();//当前显示页码  
        List<Student> list;  
          
        count = studentDao.count().intValue();  
        totalPages = count / limit;  
        if (count % limit != 0) {  
            totalPages++;  
        }  
        int currPage = Math.min(totalPages, page);  
          
        int start = currPage * limit - limit;  
        start = start < 0 ? 0 : start; 
        
        if (null != request) {
        	list = studentDao.list(start, limit, dataRequest.getSord(), dataRequest.getSidx(), 
        			request.getParameter("unitByNowGroupId"),
        			request.getParameter("unitByNewTeamId"),
        			request.getParameter("unitByNowClassId"));          	
        } else {
        	list = studentDao.list(start, limit, dataRequest.getSord(), dataRequest.getSidx(), 
        			null,null,null);
        }
        
        response.setRecords(count);  
        response.setTotal(totalPages);  
        response.setPage(currPage);  
        response.setRows(list);  
          
        return response;  
	}

	@Override
	public boolean add(Student student) {
		studentDao.insert(student);
		return true;
	}

	@Override
	public Student get(Long id) {
		return studentDao.get(id);
	}

	@Override
	public boolean update(Student student) {
		studentDao.update(student);
		return true;
	}
	@Override
	public List<Student> getAll() {
		return studentDao.getAll();
	}
}
